/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package utilclasses;

import classes.Customer;
import interfaces.Services;
import java.awt.Component;
import java.awt.Container;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.Date;
import java.util.List;
import java.util.ResourceBundle;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 * Class with methods for performing actions required by the client
 * @author keaneb1
 */
public class ClientUtils {
    private static final String SERVER_PROPS = "config.server";
    
    /**
     * Method to open a connection to the server.
     * @return the remote reference bound to the server
     */
    public static Services provideRMI(){
        ResourceBundle props;
        //read the server hostname string from the properties file
        props = ResourceBundle.getBundle(SERVER_PROPS);
        String racappServer = props.getString("server");
            
        Services stub = null;
        try {
            //obtain a reference to the remote registry object on the server
            Registry registry = LocateRegistry.getRegistry(racappServer);   
            //return the remote reference bound to RacappSrv
            stub = (Services) registry.lookup("RacappSrv"); 
            
            }
        catch(RemoteException | NotBoundException e){
            JOptionPane.showMessageDialog(null, e.getMessage());
            
        }
        return stub;
    }
    
    
    /**
     * Method to determine the number of days of rental
     * @param startDate the date when the rental period starts
     * @param endDate the date when the rental period ends
     * @return the number of days in the rental period
     */
    public static int getNrOfDaysFromDateInterval(Date startDate, Date endDate) {
        return (int) ((endDate.getTime() - startDate.getTime()) / (1000 * 60 * 60 * 24));
    }

    /**
     * Method for enabling or disabling all components of a container.
     * @param c the container which should have its components enabled or disabled
     * @param b the boolean value for the enabled state of the components
     */
    public static void setAppearance(Container c, boolean b){
        Component[] tmp = c.getComponents();
        for(int i=0;i<tmp.length;i++){
            if(!"enabler".equals(tmp[i].getName()))
            tmp[i].setEnabled(b);
        }
    }
    
    /**
     * Method called by the client to fill the customer details table
     * @param customerTBLModel stores the cell value objects for the JTable
     * @param searchString the search string if entered by the user
     * @return return the cell values which will populate the JTable.
     */
    public static DefaultTableModel fillCustomers(DefaultTableModel customerTBLModel,String searchString){
        //first clean out the current cell values    
        while(customerTBLModel.getRowCount()>0){
            customerTBLModel.removeRow(0);
        }
        //query the database for all customers using the search string.
        Services stub = provideRMI();
        if(stub!=null){
            try {
            
                List<Customer> prevCustomers = stub.getCustomers(searchString);
                for(int i=0;i<prevCustomers.size();i++){
                    Customer customer = prevCustomers.get(i);
                    //fill the TableModel object with the cell values which will be put in the JTable
                    customerTBLModel.addRow(new Object[]{customer.getCreditCard().getCardNumber(),(customer.getFirstName()+" "+customer.getLastName())});
                }
            } catch (RemoteException ex) {
                JOptionPane.showMessageDialog(null, "Problem in fillCustomers! " + ex.getMessage());
            }
        }
        return customerTBLModel;
    }
}
